﻿using CommonTools;
using CommonTools.Controls;
using shop.Bll.BasicInfo;
using shop.Bll.cost;
using shop.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Threading.Tasks;

namespace shop.cost
{
    /// <summary> 快递账户 </summary>
    public class LogiInfoBllUI : LogiInfoBll
    {
        private ControlBuilder _builder;

        public override ControlBuilder GetUIBuilder()
        {
            _builder = base.GetUIBuilder()
                .SetFlowBreak()
                .AddButton("从旺店通同步", ImportDataFromWdt, true)
                .SetFlowBreak()
                .AddButton("导入", ImportData, false)
                .AddButton("下载模板", templateDownload, false);

            return _builder;
        }

        /// <summary> 导入 </summary>
        public async Task<PageButtonResult> ImportDataFromWdt()
        {

            //从旺店通查询
   
            //var res = await WaitUI.RunAsync(async ()=> await new Bll.Logi.QueryLogisticsBll().QuerylogisticsCompany());
            var res = await new Bll.Logi.QueryLogisticsBll().QuerylogisticsCompany();
            if (res != null)
            {
                var list = res
                    .Select(a => new LogiInfo()
                    {
                        id = 0,
                        IsAgent = false,
                        isdel = false,
                        ItemId = 1,
                        LogiAcct = string.Empty,
                        Remarks = a.remark,
                        logistics_id = a.logistics_id,
                        logistics_no = a.logistics_no,
                        logistics_name = a.logistics_name,
                        LogiCompany = a.LogiCompany,
                    })
                    .ToList();

                //同步
                await SyncTo(list);

                return new PageButtonResult(false, "导入完成, 请重新查询", true);
            }
            else
            {
                return new PageButtonResult(false, "没有查询到数据", false);
            }


        }




        /// <summary> 导入 </summary>
        public async Task<PageButtonResult> ImportData()
        {
            var dt = ExcelHelper.Import();
            if (dt == null)
            {
                return new PageButtonResult(false, null, false);
            }

            //
            var batch = DateTime.Now.ToString("yyyy-MM-ddTHHmmss") + Guid.NewGuid().ToString();

            var res = await WaitUI.RunAsync(async () =>
            {
                var list = dt.AsEnumerable()
                 .Select(a => new LogiInfo()
                 {
                     LogiAcct = a.Field<string>(0),
                     logistics_name = a.Field<string>(1),
                     LogiCompany = a.Field<string>(2),
                     IsAgent = a.Field<string>(3).IsNull(false),
                     batch = batch,
                 })
                 .Where(a => string.IsNullOrEmpty(a.LogiAcct) == false || string.IsNullOrEmpty(a.logistics_name) == false || string.IsNullOrEmpty(a.LogiCompany) == false)
                 .ToList();

                var res1 = await base.SaveItem(list);

                return new PageButtonResult(res1.ErrorCode, res1.Message, true);
            });

            if (res.IsOk)
            {
                //修改旧的日期  a是旧数据
                await DB.FSql.Update<LogiInfo>()
                    .Join<LogiInfo>((a, b) => a.logistics_name == b.logistics_name && a.id != b.id 
                        && b.batch == batch && (a.batch == null || a.batch == string.Empty || a.batch != batch))
                    .Set((a, b) => a.isdel == false)
                    .Where((a, b) => a.isdel == false && b.isdel == false && b.batch == batch && (a.batch == null || a.batch == string.Empty || a.batch != batch)  )
                    .ExecuteAffrowsAsync();

                return new PageButtonResult(true, "导入完成", true);
            }
            else
            {
                return new PageButtonResult(false, res.Message);
                //MessageBox.Show(res.Message, Properties.Resources.systemInformation, MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        /// <summary>  </summary>
        public async Task<PageButtonResult> templateDownload()
        {
            var list = (new int[0])
                .Select(a => new
                {
                    快递老板 = string.Empty,
                    物流公司名称 = string.Empty,
                    公司 = string.Empty,
                    是否代发 = false,
                })
                .ToList();

            new ExcelExporter()
                .NewSheet(null)
                .AddList(list)
                .EndThenOpen("快递账户模板");

            return await Task.FromResult(new PageButtonResult(false, null, false));
        }



    }
}
